//
//  YBSelectPeopleOftenUseView.m
//  GongXinDangJian
//
//  Created by apple on 17/11/30.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBSelectPeopleOftenUseView.h"
#import "YBSelectPeopleNormalTableViewCell.h"
#define CELLID @"YBSelectPeopleNormalTableViewCell"

#import "YBSelectPeopleHeadView.h"
#define HEADER @"YBSelectPeopleHeadView"

#import "YBSelectShareSingalData.h"


@interface YBSelectPeopleOftenUseView ()<UITableViewDelegate , UITableViewDataSource , YBSelectPeopleHeadViewDelegate>
@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , assign) BOOL isKVO;


@property (nonatomic , strong) YBSelectShareSingalData * myDataSource;

@end

@implementation YBSelectPeopleOftenUseView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self ybInitMyView];
        [self addObserver:self forKeyPath:@"ybSelectChange" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
        self.isKVO = YES;
    }
    return self;
}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectedArrayDidChange)]) {
        [self.delegate selectedArrayDidChange];
    }
}

-(void)ybCleanObserver{
    if (self.isKVO) {
        [self removeObserver:self forKeyPath:@"ybSelectChange" context:nil];
        self.isKVO = NO;
    }
}


-(void)removeFromSuperview{
    if (self.isKVO) {
        [self removeObserver:self forKeyPath:@"ybSelectChange" context:nil];
        self.isKVO = NO;
    }
    [super removeFromSuperview];
}

-(void)setYbIsHidden:(BOOL)ybIsHidden
{
    _ybIsHidden = ybIsHidden;
    self.hidden = ybIsHidden;
}


-(void)ybInitMyView{
    [self addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.headArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    YBSelectHeadModel * tempModel = self.headArray[section];
    if (tempModel.ISOPEN) {
        NSArray * tempArray = self.dataArray[section];
        return tempArray.count;
    }
    else{
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ItemHeight * 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBSelectPeopleNormalTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.section][indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBPeopleInfoModel * model = self.dataArray[indexPath.section][indexPath.row];
    model.ISSELECT = !model.ISSELECT;
    YBSelectPeopleNormalTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.cellSelected = !cell.cellSelected;
    [self judgeIsSelectAllWithSection:indexPath.section];
    if (model.ISSELECT) {
        [self.myDataSource ybAddObjectToArrayWithModel:model];
    }
    else{
        [self.myDataSource ybRemoveObjectFromArrayWithModel:model];
    }
    [self mySelectArrayChanged];
}


-(void)judgeIsSelectAllWithSection:(NSInteger)section{
    BOOL headIsSelectAll = NO;
    NSInteger selectCount = 0;
    NSArray * tempArray = [self.dataArray[section] copy];
    for (YBPeopleInfoModel * model in tempArray) {
        if (model.ISSELECT) {
            selectCount ++;
        }
    }
    if (tempArray.count == selectCount) {
        headIsSelectAll = YES;
    }
    YBSelectPeopleHeadView * headerView = (YBSelectPeopleHeadView *)[self.tableView headerViewForSection:section];
    headerView.viewSelected = headIsSelectAll;
    ((YBSelectHeadModel *)(self.headArray[section])).ISALLSELECT = headIsSelectAll;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return ItemHeight * 80;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YBSelectPeopleHeadView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HEADER];
    headerView.ybSection = section;
    headerView.model = self.headArray[section];
    headerView.delegate = self;
    return headerView;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


-(void)viewDidClickWithSection:(NSInteger)section isOpen:(BOOL)isOpen{
    ((YBSelectHeadModel *)(self.headArray[section])).ISOPEN = isOpen;
    [self.tableView reloadData];
}

-(void)viewDidClickWithSection:(NSInteger)section viewSelected:(BOOL)viewSelected{
    ((YBSelectHeadModel *)(self.headArray[section])).ISALLSELECT = viewSelected;
    
    for (YBPeopleInfoModel * model in self.dataArray[section]) {
        model.ISSELECT = viewSelected;
        if (viewSelected) {
            [self.myDataSource ybAddObjectToArrayWithModel:model];
        }
        else{
            [self.myDataSource ybRemoveObjectFromArrayWithModel:model];
        }
    }
    [self.tableView reloadData];
    [self mySelectArrayChanged];
}




-(void)mySelectArrayChanged{
    self.ybSelectChange = !self.ybSelectChange;
}


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStyleGrouped];
        [_tableView registerClass:[YBSelectPeopleNormalTableViewCell class] forCellReuseIdentifier:CELLID];
        [_tableView registerClass:[YBSelectPeopleHeadView class] forHeaderFooterViewReuseIdentifier:HEADER];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView adaptiveIOS11];
        
    }
    return _tableView;
}


-(void)loadDataWithDataArray:(NSMutableArray *)dataArray headArray:(NSMutableArray *)headArray
{
    self.dataArray = [dataArray mutableCopy];
    NSMutableArray * tempHeadArray = [headArray mutableCopy];
    for (YBSelectHeadModel * model in self.headArray) {
        [headArray enumerateObjectsUsingBlock:^(YBSelectHeadModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.ID isEqualToString:model.ID]) {
                ((YBSelectHeadModel *)tempHeadArray[idx]).ISOPEN = model.ISOPEN;
            }
        }];
    }
    self.headArray = [tempHeadArray mutableCopy];
    [self.tableView reloadData];
}



-(void)ybReloadData{
    NSArray * idArray = [self.myDataSource getCurrentSelectIdArray];
    [self.dataArray enumerateObjectsUsingBlock:^(NSMutableArray *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger selectedCount = 0;
        for (YBPeopleInfoModel * tempModel in obj) {
            if ([idArray containsObject:tempModel.ID]) {
                selectedCount ++;
                tempModel.ISSELECT = YES;
            }
            else{
                tempModel.ISSELECT = NO;
            }
        }
        YBSelectHeadModel * tempHeadModel = self.headArray[idx];
        if (selectedCount == obj.count) {
            tempHeadModel.ISALLSELECT = YES;
        }
        else{
            tempHeadModel.ISALLSELECT = NO;
        }
    }];
    [self.tableView reloadData];
}


-(YBSelectShareSingalData *)myDataSource
{
    if (!_myDataSource) {
        _myDataSource = [YBSelectShareSingalData getYBSelectShareSingalData];
    }
    return _myDataSource;
}

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


-(NSMutableArray *)headArray
{
    if (!_headArray) {
        _headArray = [NSMutableArray array];
    }
    return _headArray;
}

@end
